import requests
from lxml import etree

# 初始 URL
urls = "https://chejiahao.autohome.com.cn/Authors/AuthorListMore?orderType=3&page=1&userCategory=13"
cookies = {
    'sessionid': '8C01F35C-3C4D-4418-A51B-40A34283B9A0%7C%7C2024-03-08+16%3A00%3A02.507%7C%7C0',
    'autoid': '5d614292dadb60239857002f5c8defde',
    '__ah_uuid_ng': 'c_8C01F35C-3C4D-4418-A51B-40A34283B9A0',
    'fvlid': '1709884921398Q2m8JqxcSA',
    '_ac': '9116e3ef98329a17.1741433233',
    'sessionip': '111.85.200.39',
    'sessionvid': '68F9D97A-9FBD-4660-9941-6FF84C2A1059',
    'area': '520199',
    'cookieCityId': '520100',
    'ahpvno': '4',
    'pvidchain': '6863948,3311237,2808104',
    'v_no': '4',
    'visit_info_ad': '8C01F35C-3C4D-4418-A51B-40A34283B9A0||68F9D97A-9FBD-4660-9941-6FF84C2A1059||-1||-1||4',
    'ref': 'www.so.com%7C0%7C0%7C0%7C2025-03-08+19%3A28%3A26.993%7C2025-03-08+19%3A27%3A13.261',
    'ahrlid': '1741433304806SoYHX3O8GJ-1741433316472',
}

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'max-age=0',
    'priority': 'u=0, i',
    'referer': urls,
    'sec-ch-ua': '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'document',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
}

# 获取作者列表页面
response = requests.get(urls, cookies=cookies, headers=headers).text
resqutst = etree.HTML(response)
url_lists = resqutst.xpath('//div[@class="list-box"]/a/@href')

# 拼接完整 URL
base_url = "https://chejiahao.autohome.com.cn"
full_urls = [base_url + url for url in url_lists]

# 遍历每个完整 URL 并提取粉丝数和用户作品
for full_url in full_urls:
    # 请求作者详情页面
    author_response = requests.get(full_url, cookies=cookies, headers=headers).text
    author_html = etree.HTML(author_response)

    # 获取用户名
    username = author_html.xpath('/html/body/div[3]/div[1]/div[2]/div[2]/div[1]/div[1]/text()')
    username = username[0] if username else 'N/A'

    # 提取粉丝数
    fans_count = author_html.xpath('/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/span[1]/strong/text()')
    fans_count = fans_count[0] if fans_count else 'N/A'

    # 提取用户作品数
    works_count = author_html.xpath('/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/span[3]/strong/text()')
    works_count = works_count[0] if works_count else 'N/A'

    # 提前作品链接
    works_url_house = author_html.xpath('/html/body/div[3]/div[2]/div[3]/div[1]/div[1]/a/@href')
    print(works_url_house)
    works_url_house = works_url_house[0] if works_url_house else 'N/A'
    works_url = "https://chejiahao.autohome.com.cn"+works_url_house

    # 打印结果
    print(f"用户名: {username}")
    print(f"粉丝数: {fans_count}")
    print(f"用户作品数: {works_count}")
    print(f"作品URL: {works_url}")
    print(f"URL: {full_url}")
    print("-" * 40)
